import axios from "axios";
import Vue from "vue";
import router from "@/router/index";
import Axios from "axios";
import { Message } from "element-ui";
import cookie from "@/utils/cookie";
// import { Loading } from 'element-ui';

// axios.defaults.headers.post['Content-Type'] = 'application/json';

//
// axios.defaults.withCredentials = true; // 让ajax携带cookie

// axios.defaults.baseURL = 'http://192.168.1.172:8001';//本地测试
// Vue.prototype.pathImg = 'http://192.168.1.172:8001';
// Vue.prototype.wsurl = 'ws://192.168.1.172:8001';

//根据打包时参数不同，使用不同url
axios.defaults.baseURL = '/api/shop/admin'
Vue.prototype.wsurl = "ws://" + location.host;
// if (process.env.PACK_TYPE === "prod") {
//   axios.defaults.baseURL =
//     process.env.NODE_ENV === "development" ? "/shop/admin" : "/shop/admin"; //正式ip
//   Vue.prototype.pathImg =
//     process.env.NODE_ENV === "development" ? "/shop/admin" : "/shop/admin";
//   Vue.prototype.wsurl = "ws://" + location.host;
// } else {
//   axios.defaults.baseURL =
//     process.env.NODE_ENV === "development" ? "/shop/admin" : "/shop/admin"; //测试打包ip
//   Vue.prototype.pathImg =
//     process.env.NODE_ENV === "development" ? "/shop/admin" : "/shop/admin";
//   Vue.prototype.wsurl = "ws://" + location.host;
// }

// axios.defaults.timeout = 200000; //请求超时时间
axios.defaults.withCredentials = true;
axios.interceptors.request.use(
  function(config) {
    //如果cookie里面有值 就传token
    if (cookie.getCookie("authorization")) {
      config.headers.common["authorization"] = cookie.getCookie(
        "authorization"
      );
    }
    if (cookie.getCookie("appId")) {
      config.headers.common["appId"] = cookie.getCookie("appId");
    }
    return config;
  },
  function(error) {
    return Promise.reject(error);
  }
);
axios.interceptors.response.use(
  response => {
    if (
      response.data.resultCode == 12001 ||
      response.data.resultCode == 12003 ||
      response.data.resultCode == 12005 ||
      response.data.resultCode == 12006
    ) {
      sessionStorage.clear();
      localStorage.clear();
      cookie.delCookie("authorization");
      cookie.delCookie("appId");
      cookie.delCookie("Islogin");
      router.push("/login");
    }
    return response;
  },
  error => {
    if (error.response == undefined) {
      return {
        data: {
          success: false,
          msg: "请求异常"
        }
      };
    } else {
      return checkStatus(error.response);
    }

    // 返回接口返回的错误信息
  }
);
// 检查状态
function checkStatus(response) {
  let data = {
    data: {}
  };
  data.data["success"] = false;
  switch (response.status) {
    case 400:
      data.data["resultInfo"] = "请求错误";
      break;

    case 401:
      data.data["resultInfo"] = "未授权，请登录";
      break;

    case 403:
      data.data["resultInfo"] = "拒绝访问";
      break;

    case 404:
      data.data["resultInfo"] = "请求地址出错";
      break;

    case 408:
      data.data["resultInfo"] = "请求超时";
      break;

    case 500:
      data.data["resultInfo"] = "服务器内部错误";
      break;

    case 501:
      data.data["resultInfo"] = "服务未实现";
      break;

    case 502:
      data.data["resultInfo"] = "网关错误";
      break;

    case 503:
      data.data["resultInfo"] = "服务不可用";
      break;

    case 504:
      data.data["resultInfo"] = "网关超时";
      break;

    case 505:
      data.data["resultInfo"] = "HTTP版本不受支持";
      break;

    default:
      data.data["resultInfo"] = "异常请求";
  }
  return data;
}

export default axios;
let $post = function(params) {
  return Axios({
    method: "post",
    ...params
  }).then(res => {
    if (res.data.success) {
      return res.data.result;
    } else {
      if (!params.notToastErr && res.data && res.data.resultInfo) {
        Message.warning(res.data.resultInfo);
      }
      return Promise.reject(res.data);
    }
  });
};

let $download = function(params) {
  return Axios({
    method: "post",
    responseType: "blob",
    ...params
  }).then(res => {
    if (res.status === 200) {
      return res.data;
    } else {
      if (!params.notToastErr && res.data && res.data.resultInfo) {
        Message.warning(res.data.resultInfo);
      }
      return Promise.reject(res.data);
    }
  });
};

let $get = function(params) {
  return Axios({
    method: "get",
    ...params
  }).then(res => {
    if (res.data.success) {
      return res.data.result;
    } else {
      if (!params.notToastErr && res.data && res.data.resultInfo) {
        Message.warning(res.data.resultInfo);
      }
      return Promise.reject(res.data);
    }
  });
};
export { $post, $get, $download };
